// pages/recruit/jobDetail/jobDetail.js
const app = getApp();
const time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 基本参数
    recruit_info: [],
    flag_group: [],
    plat_name: [],

    //传参
    status: '',
    deatil_id: '',
    check_number_real: '',
    share_number: '',
    anchor_send_resume: '',

    //弹框
    flag_contact: true,
  },
  //go_company
  go_company(e) {
    // console.log(e.currentTarget.dataset.home_url)
    let company_name = e.currentTarget.dataset.company_name,
      token = e.currentTarget.dataset.token;
    if (company_name) {
      wx.navigateTo({
        url: '../../mine/anchorWelfare/checkDetal/checkDetal?token=' + token,
      })
      // wx.navigateTo({
      //   url: '../../protocol/protocol?url=' + url + '&title=公司详情',
      // })
    } else {
      wx.showToast({
        title: '该公司暂未完善主页信息',
        icon: 'none',
      })
    }

  },
  //分享
  click_share() {
    wx.getShareInfo({})
  },
  //删除职位信息
  click_del() {
    let that = this;
    wx.showModal({
      content: '您确定要删除该职位么？删除后，该职位将不在职位列表中出现。',
      confirmColor: '#d0a76c',
      success(e) {
        // console.log(e.confirm)
        if (e.confirm) {
          // console.log("点击了确定")
          // console.log(that.data.recruit_id)
          wx.request({
            url: app.globalData.url + 'api/guild/update/job/status',
            data: {
              token: app.globalData.token,
              recruit_id: that.data.recruit_id,
              status: 5,
            },
            method: 'POST',
            success: function(e) {
              console.log(e)
              wx.redirectTo({
                url: '../jobList/jobList',
              })
            },
          })
        } else {}
      }
    })
  },
  //修改职位信息
  click_update() {
    wx.navigateTo({
      url: '../../recruit/postJob/postJob?recruit_id=' + this.data.recruit_id + "&type_name=" + this.data.recruit_info.type_name + "&plat_name=" + this.data.recruit_info.plat_name + "&changed=true",
    })
  },
  //改变职位状态
  job_status() {
    let that = this;
    let status = this.data.status;
    if (status == "1") {
      //正常
      var _status = "4";
      wx.showToast({
        title: '已成功关闭该职位',
        icon: 'none',
      })
    } else if (status == "3") {
      //审核失败
      var _status = "2";
      wx.redirectTo({
        url: '../jobList/jobList',
      })
      wx.showToast({
        title: '审核中',
        icon: 'none',
      })
    } else if (status == "4") {
      //已关闭
      var _status = "1";
      wx.showToast({
        title: '已成功开放该职位',
        icon: 'none',
      })
    } else if (status == "2") {
      wx.showToast({
        title: '审核中',
        icon: 'none',
      })
    }

    wx.request({
      url: app.globalData.url + 'api/guild/update/job/status',
      data: {
        token: app.globalData.token,
        recruit_id: this.data.deatil_id,
        status: _status,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)

      },
    })
    //更新状态
    this.recruit_list(this.data.deatil_id)


  },
  //隐藏弹框
  modal_hide() {
    this.setData({
      flag_contact: true,
    })
  },
  //升级按钮
  go_shengji() {
    // this.setData({
    //   flag_contact: false,
    // })
    if(this.data.status==4){
      wx.showToast({
        title: '请先开放职位',
        icon: 'none'
      })
    }else{
      wx.navigateTo({
        url: '../../payment/postBar/index?recruit_id=' + this.data.deatil_id
      })
    }
    
  },
  //招聘信息
  recruit_list: function(deatil_id) {
    let that = this;
    that.setData({
      flag_group: [],
    })
    wx.request({
      url: app.globalData.url + 'api/read/recruit/xcx/detail',
      method: "POST",
      data: {
        token: app.globalData.token,
        recruit_id: deatil_id,
      },
      success: function(e) {
        // console.log(e.data.data.xcx)
        //设置---更新状态
        that.setData({
          status: e.data.data.xcx["0"].status,
        })
        // console.log("now+" + that.data.status)
        if (!e.data.data.xcx[0].job_description) {
          // console.log(e)
        } else {
          let strs1 = (e.data.data.xcx[0].job_description).toString();
          // console.log(strs1)

          let strs_br = strs1.replace(/<br\/>/g, "\n");

          let strs11 = strs_br.replace(/<\/?.+?>/g, "");
          let job_description = strs11.replace(/&nbsp;/g, ""); //str为得到后的内容
          that.setData({
            job_description: job_description,
          })
        }

        if (!e.data.data.xcx[0].company_description) {
          // console.log(e)
        } else {
          let strs2 = (e.data.data.xcx[0].company_description).toString();

          let strs_br = strs2.replace(/<br\/>/g, "\n");

          let strs22 = strs_br.replace(/<\/?.+?>/g, "");
          let company_description = strs22.replace(/&nbsp;/g, ""); //str为得到后的内容
          that.setData({
            company_description: company_description,
          })
        }
        if (!e.data.data.xcx[0].anchor_send_resume) {
          var anchor_send_resume = 0;
        } else {
          var anchor_send_resume = e.data.data.xcx[0].anchor_send_resume;
        }
        if (!e.data.data.xcx[0].check_number_real) {
          var check_number_real = 0;
        } else {
          var check_number_real = e.data.data.xcx[0].check_number_real;
        }
        if (!e.data.data.xcx[0].share_number) {
          var share_number = 0;
        } else {
          var share_number = e.data.data.xcx[0].share_number;
        }
        that.setData({
          anchor_send_resume: anchor_send_resume,
          check_number_real: check_number_real,
          share_number: share_number,
        })


        // console.log(e.data.data.xcx[0].pay_low)
        //pay_low
        if (e.data.data.xcx[0].pay_low > 1000) {
          var low_str = (e.data.data.xcx[0].pay_low).toString();
          var pay_low = low_str.substring(0, low_str.length - 3) + "k";
        } else {
          var pay_low = (e.data.data.xcx[0].pay_low).toString();
        }

        //pay_high
        if (e.data.data.xcx[0].pay_low > 1000) {
          var high_str = (e.data.data.xcx[0].pay_high).toString();
          var pay_high = high_str.substring(0, high_str.length - 3) + "k";
        } else {
          var pay_high = (e.data.data.xcx[0].pay_high).toString();
        }

        //keep_pay
        if (e.data.data.xcx[0].keep_pay > 1000) {
          var keep_str = (e.data.data.xcx[0].keep_pay).toString();
          var keep_pay = keep_str.substring(0, keep_str.length - 3) + "k";
        } else {
          var keep_pay = (e.data.data.xcx[0].keep_pay).toString();
        }

        //平台
        // console.log(e.data.data.xcx[0].plat_name)
        if (e.data.data.xcx[0].plat_name) {
          let plat_name = e.data.data.xcx[0].plat_name.split("|").join(" | ");
          that.setData({
            plat_name: plat_name,
          })
        } else {}

        //线上线下
        // console.log(e.data.data.xcx[0].job_method)
        if (e.data.data.xcx[0].job_method) {
          console.log(e.data.data.xcx[0].job_method)
          let method_num = e.data.data.xcx[0].job_method;
          console.log("job_method+" + method_num)

          switch (method_num) {
            case "1":
              var job_method = "线上";
              break;
            case "2":
              var job_method = "线下";
              break;
            case "3":
              var job_method = "线上/线下";
              break;
            default:
              var job_method = "";
          }
          let flag_group = that.data.flag_group;
          flag_group.push(job_method)
          that.setData({
            flag_group: flag_group,
          })
          // console.log(that.data.flag_group)
        } else {}

        //结算方式
        // console.log(e.data.data.xcx[0].salary_type)
        if (e.data.data.xcx[0].salary_type) {
          var salary_num = e.data.data.xcx[0].salary_type;
          switch (salary_num) {
            case "1":
              var salary_type = "月结";
              break;
            case "2":
              var salary_type = "周结";
              break;
            case "3":
              var salary_type = "日结";
              break;
            case "4":
              var salary_type = "日结/周结/月结";
              break;
            default:
              var salary_type = "";
          }
          var flag_group = that.data.flag_group;
          flag_group.push(salary_type)
          console.log(flag_group)
          that.setData({
            flag_group: flag_group,
          })
          // console.log(that.data.flag_group)
        } else {}

        //福利
        // console.log(e.data.data.xcx[0].welfare)
        if (e.data.data.xcx[0].welfare) {
          //  console.log(e.data.data.xcx[0].welfare)
          let welfare_regBef = e.data.data.xcx[0].welfare;
          let welfare_regAft = welfare_regBef.replace(/，|,|、/g, ",");
          let welfare = welfare_regAft.split(",");

          let flag_group = that.data.flag_group;
          flag_group.push(...welfare)
          that.setData({
            flag_group: flag_group,
          })
        } else {}

        var expire_time = e.data.data.expire_time,
          now_time = Date.parse(new Date()) / 1000,
          time_h = Math.floor((expire_time - now_time) / 3600),
          time_m = Math.floor(((expire_time - now_time)/60) % 60);

        //发布时间
        var recruit_info = e.data.data.xcx[0];
        recruit_info.publish_time = time.formatTimeTwo(recruit_info.publish_time, 'Y.M.D');
        
        console.log(expire_time - now_time)
        that.setData({
          is_buy: e.data.data.is_buy,
          expire_time: expire_time,
          time_h: time_h,
          time_m: time_m,
          recruit_info: recruit_info,
          recruit_phone: e.data.data.xcx[0].contact_phone,
          recruit_id: e.data.data.xcx["0"].id,

          pay_low: pay_low,
          pay_high: pay_high,
          keep_pay: keep_pay,
        })
      }
    })
  },
  //模态框 拨打
  click_phone: function() {
    //管理员、会长
    wx.makePhoneCall({
      phoneNumber: '0571-87081736',
    })
    this.setData({
      flag_contact: true,
    })
  },
  //模态框 复制
  click_modal_copy: function() {
    wx.setClipboardData({
      data: 'zhibozhijia888',
      success: function(e) {
        // wx.showToast({
        //   title: '复制成功',
        //   icon:'none'
        // })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    console.log(options)
    // console.log(options.deatil_id)
    // console.log(options.status)
    if (options.deatil_id) {
      that.setData({
        deatil_id: options.deatil_id,
        status: options.status,
        check_number_real: options.check_number_real,
        share_number: options.share_number,
        anchor_send_resume: options.anchor_send_resume
      })
    }
    this.recruit_list(this.data.deatil_id);

    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    setTimeout(()=>{
      var expire_time = this.data.expire_time;
      if (expire_time > 0) {
        setInterval(()=>{
          var now_time = Date.parse(new Date())/1000;
          var time_h = Math.floor((expire_time - now_time) / 3600),
            time_m = Math.ceil(((expire_time - now_time) / 60) % 60);
          this.setData({
            expire_time: expire_time,
            time_h: time_h,
            time_m: time_m
          })
        }, 60000)
      }
    },1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})